﻿using System;
using System.Web;

namespace UDS
{
    /// <summary>
    /// Provides some helpful methods for handling query strings.
    /// </summary>
    public static class QSUtil
    {
        /// <summary>
        /// Gets a 32-bit positive integer from the query string.
        /// </summary>
        public static int GetPositiveInt32(string name, int defaultValue)
        {
            int? value = GetInt32(name, false, false, true);
            if (value == null)
            {
                return defaultValue;
            }
            return value.Value;
        }

        /// <summary>
        /// Gets a 32-bit positive integer from the query string. Return 0 for invalid value.
        /// </summary>
        public static int GetPositiveInt32(string name)
        {
            int? value = GetInt32(name, false, false, true);
            if (value == null)
            {
                return 0;
            }
            return value.Value;
        }

        public static int GetInt32(string name, int defaultValue)
        {
            int? value = GetInt32(name, true, true, true);
            if (value != null)
            {
                return value.Value;
            }
            return defaultValue;
        }

        /// <summary>
        /// Gets a 32-bit integer from the query string.
        /// </summary>
        public static int? GetInt32(string name, bool allowNegative, bool allowZero, bool allowPositive)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            HttpContext httpContext = HttpContext.Current;
            if (httpContext != null && httpContext.Request != null)
            {
                int value;
                if (int.TryParse(httpContext.Request.QueryString[name], out value))
                {
                    if ((value > 0 && allowPositive) || (value < 0 && allowNegative) || (value == 0 && allowZero))
                    {
                        return value;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Gets a bool value from the query string.
        /// </summary>
        public static bool GetBoolean(string name, bool defaultValue)
        {
            bool? value = GetBoolean(name);
            if (value.HasValue)
            {
                return value.Value;
            }

            return defaultValue;
        }

        /// <summary>
        /// Gets a bool value from the query string.
        /// </summary>
        public static bool? GetBoolean(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            HttpContext httpContext = HttpContext.Current;
            if (httpContext != null && httpContext.Request != null)
            {
                string str = httpContext.Request.QueryString[name];
                if (string.IsNullOrEmpty(str))
                {
                    return null;
                }

                bool value;
                if (bool.TryParse(str, out value))
                {
                    return value;
                }

                int integer;
                if (int.TryParse(str, out integer))
                {
                    return integer != 0;
                }
            }
            return null;
        }
    }
}